home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 007 / circle.pas < prev    next >
Pascal/Delphi Source File  |  1985-06-03  |  4KB  |  75 lines

  1. (*****************************************************************************)
  2. (*  Plot circle of radius "radius" with origin (x_coor,y_coor)               *)
  3. (*  (( written by Bill Gaupp 1-Dec-1984 ))                                   *)
  4. (*       This method breaks a circle into 8 sectors, each with a 45 degree   *)
  5. (*  central angle.  For each i measured along a perpendicular to one of the  *)
  6. (*  sides of a sector up to the farthest point still within the sector, a    *)
  7. (*  corresponding distance j is calculated such that i and j form the sides  *)
  8. (*  of a right triangle with hypotenuse "radius".  Each of these triangles   *)
  9. (*  can be inscribed in the desired circle with one of the non-right-angle   *)
  10. (*  points at the center and the other on the circle.  Points are plotted an *)
  11. (*  offset of (i,j) or (j,i) from the coordinates given for the center of    *)
  12. (*  the circle for each of the eight sectors.  For a rectangular frame       *)
  13. (*  buffer, these are the best-approximation points for a circle.            *)
  14. (*                                                                           *)
  15. (*****************************************************************************)
  16. PROCEDUR┼ Circle¿ x¼ y¼ radiu≤ ║ INTEGER╗ set_pixel : BOOLEAN;
  17.                                           VAR frame : Frame_Buffer );
  18. VAR
  19.   radius_squared : REAL;
  20.   i, j : INTEGER;
  21. BEGIN { Circle }
  22.   radius_squared := radius * radius;
  23.   FOR i := 0 TO ROUND( radius / SQRT(2) ) DO
  24.     BEGIN
  25.       j := ROUND( SQRT( radius_squared - i*i ) );
  26.       Put_Pixel¿ x ½ j¼ y - i¼ set_pixel¼ framσ )╗ √ quadran⌠ 1¼ bottoφ quad }
  27.       Put_Pixel( x + i, y - j, set_pixel, frame ); { quadrant 1,    top quad }
  28.       Put_Pixel( x - i, y - j, set_pixel, frame ); { quadrant 2,    top quad }
  29.       Put_Pixel( x - j, y - i, set_pixel, frame ); { quadrant 2, bottom quad }
  30.       Put_Pixel( x - j, y + i, set_pixel, frame ); { quadrant 3,    top quad }
  31.       Put_Pixel( x - i, y + j, set_pixel, frame ); { quadrant 3, bottom quad }
  32.       Put_Pixel( x + i, y + j, set_pixel, frame ); { quadrant 4, bottom quad }
  33.       Put_Pixel¿ ° ½ j¼ y ½ i¼ set_pixel¼ framσ )╗ √ quadran⌠ 4¼    top quad }
  34.     END; { for }
  35. END; { Circle }
  36.  
  37.  
  38. (*****************************************************************************)
  39. (*  Draw a line from point (x1,y1) to point (x2,y2)                          *)
  40. (*  (( written by Bill Gaupp 1-Dec-1984 ))                                   *)
  41. (*       Method used is to move from point (x1,y1) to (x2,y2) in n steps,    *)
  42. (*  where n equals the length of the longest projection of the line onto     *)
  43. (*  the two axis.  This method will visit every pixel between (x1,y1) and    *)
  44. (*  (x2,y2) on a rectangular frame buffer.  Trivial (fast) case of           *)
  45. (*  adjacent pixels is handled first for speed.                              *)
  46. (*                                                                           *)
  47. (*****************************************************************************)
  48. PROCEDUR┼ Line¿ x1¼ y1¼ x2¼ y▓ ║ INTEGER╗ set_pixe∞ ║ BOOLEAN;
  49.                                           VAR frame : Frame_Buffer );
  50. VAR
  51.   length, i : INTEGER;
  52.   x_step, y_step, x, y : REAL;
  53. BEGIN
  54.   IF ( ABS(x1 - x2) <= 1 ) AND ( ABS(y1 - y2) <= 1 )
  55.     THEN
  56.       BEGIN  { adjacent pixels }
  57.         Put_Pixel( x1, y1, set_pixel, frame );
  58.         Put_Pixel( x2, y2, set_pixel, frame );
  59.       END
  60.     ELSE
  61.       BEGIN
  62.         IF ABS(x2 - x1) > ABS(y2 - y1)
  63.           THEN length := ABS(x2 - x1)  { horizontal sweep }
  64.           ELSE length := ABS(y2 - y1);   { vertical sweep }
  65.         x := x1;  x_step := (x2 - x1) / length;  { initial and }
  66.         y := y1;  y_step := (y2 - y1) / length;  {  increment size }
  67.         FOR i := 0 TO length DO
  68.           BEGIN
  69.             Put_Pixel( ROUND(x), ROUND(y), set_pixel, frame );
  70.             x := x + x_step;
  71.             y := y + y_step;
  72.           END; { for }
  73.       END; { if }
  74. END; { Line }
  75.